package web;

import service.impl.ProductServiceImpl;
import ssm.pojo.Cart;
import ssm.pojo.CartItem;
import ssm.pojo.Product;
import ssm.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//购物车
@WebServlet("/cart")
public class CartServlet extends BeanUser {

    public Cart getCart(HttpServletRequest request) {
        Cart car = (Cart) request.getSession().getAttribute("cart");
        if (car == null) {
            Cart cart = new Cart();
            request.getSession().setAttribute("cart", cart);
            return cart;
        }
        return car;

    }

    //添加购物车
    public void addCarts(HttpServletRequest request, HttpServletResponse response) {

        try {
            User user = (User) request.getSession().getAttribute("user");
            if(user==null){
                request.setAttribute("msg","抱歉,您还没有登录,请<a href='http://localhost:8080/11_22_war/jsp/login.jsp'>登录</a>");
                request.getRequestDispatcher("/jsp/jihuo.jsp").forward(request,response);
            }
            //商品id
            String id = request.getParameter("pid");
            ProductServiceImpl ps = new ProductServiceImpl();
            Product product = ps.findPById(id);

            //商品数量
            String quantity = request.getParameter("quantity");
            int count = Integer.parseInt(quantity);

            CartItem cartItem = new CartItem(product, count);
            Cart cart = getCart(request);
            cart.addCartItem(cartItem);
            response.sendRedirect(request.getContextPath() + "/jsp/cart.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }

    }

    //删除商品
    public void deleteItem(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid");
            Cart cart = getCart(request);
            cart.deleteCartItem(pid);
            response.sendRedirect(request.getContextPath() + "/jsp/cart.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //清空购物车
    public void cleanCart(HttpServletRequest request, HttpServletResponse response){
        try {
            Cart cart = getCart(request);
            cart.cleanCartItem();
            response.sendRedirect(request.getContextPath() + "/jsp/cart.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void noCart(HttpServletRequest request, HttpServletResponse response){
        try {
            request.setAttribute("msg","亲,购物车什么都没有~");
            request.getRequestDispatcher("/jsp/jihuo.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
